package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.Clazz;
import com.example.wechatdaka.entity.UserStudent;
import com.example.wechatdaka.entity.UserTeacher;
import com.example.wechatdaka.service.ClazzService;
import com.example.wechatdaka.service.UserStudentService;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-26
 */
@Api(tags = "班级模块")
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private UserStudentService userStudentService;
    @ApiOperation(value = "选择教师")
    @RequestMapping(value = "/selectTeachers",method = RequestMethod.POST)
    public Result selectTeacherList(){
        List<UserTeacher> clazzList = clazzService.selectTeacherList();
        return new Result(Result.SUCCESS,clazzList,"success");
    }
    @ApiOperation(value = "查看所有班级")
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result queryAllClazz(){
        List<Clazz> clazzList = clazzService.queryAllClazz();
        return new Result(Result.SUCCESS,clazzList,"success");
    }
    @ApiOperation(value = "分页查看所有班级")
    @RequestMapping(value = "/indexByPage",method = RequestMethod.GET)
    public JsonResult queryAllByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize) {
        Page<Clazz> page = new Page<>(currentPage, pagesize);
        IPage<Clazz> userWechatIPage = clazzService.queryAllClazzPage(page);
        List<Clazz> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "分页根据教师id查看所有班级")
    @RequestMapping(value = "/queryByTeacherPage",method = RequestMethod.GET)
    public JsonResult queryClazzByTeacherPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String id) {
        Page<Clazz> page = new Page<>(currentPage, pagesize);
        IPage<Clazz> userWechatIPage=new Page<>();
        if("1".equals(id)){
            userWechatIPage = clazzService.queryAllClazzPage(page);
        }else {
            userWechatIPage = clazzService.queryClazzByTeacherPage(page,id);
        }

        List<Clazz> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }

    @ApiOperation(value = "分页根据关键字查看所有班级")
    @RequestMapping(value = "/queryByKeywordPage",method = RequestMethod.GET)
    public JsonResult queryClazzByKeywordPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String keyword) {
        Page<Clazz> page = new Page<>(currentPage, pagesize);
        IPage<Clazz> userWechatIPage = clazzService.queryClazzByKeywordPage(page,keyword);
        List<Clazz> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "添加/更新班级")
    @RequestMapping(value = "/saveOrUpdateClazz",method = RequestMethod.POST)
    public Result saveOrUpdateClazz(@RequestBody Clazz clazz){
        boolean saveOrUpdate = clazzService.saveOrUpdate(clazz);
        if (saveOrUpdate){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"ERROR");
    }
    @ApiOperation(value = "删除班级")
    @RequestMapping(value = "/deleteClazz",method = RequestMethod.POST)
    public Result deleteClazz(@RequestBody String id){
        boolean removeById = clazzService.removeById(id);
        QueryWrapper<UserStudent> userStudentQueryWrapper=new QueryWrapper<>();
        userStudentQueryWrapper.eq("lxClazzId",id);
        boolean remove = userStudentService.remove(userStudentQueryWrapper);
        if (removeById&&remove){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"ERROR");
    }
}

